import json

from django.views.decorators.csrf import csrf_exempt

from app02 import models
from django import forms
from django.shortcuts import render, HttpResponse
from app02.utils.bootstrap import BootstrapModelForm


class TaskModelForm(BootstrapModelForm):
    class Meta:
        model = models.Task
        fields = '__all__'
        widgets = {
            "detail": forms.TextInput()
        }


def task_list(request):
    """ 任务列表 """
    form = TaskModelForm()
    return render(request, "task_list.html", {'form': form})


@csrf_exempt
def task_add(request):
    form = TaskModelForm(request.POST)
    if form.is_valid():
        form.save()
        data_dict = {"status": True}
        return HttpResponse(json.dumps(data_dict))